// pages/user/user.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: [],
    currentSize: 0,
    avatar: '',
    visible: false,
    actionSheetVisible: false,
    actionSheetList: [
      // {
      //   name: '男',
      // },
      {
        name: '女'
      },
      {
        name: '动漫男',
        // icon: 'share',
        // openType: 'share'
      },
      {
        name: '动漫女'
      },
    ],
    actionSheetActive: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      currentSize: wx.getStorageInfoSync().currentSize
    })
    this.getData()
  },
  getData() {
    wx.request({
      url: app.$api.avatarInterface,
      data: {
        // 分类[男|女|动漫男|动漫女]
        sort: this.data.actionSheetList[this.data.actionSheetActive].name
      },
      success: res => {
        console.log(res);
        if (res.statusCode == 200 && res.data.imgurl) {
          this.setData({
            avatar: app.$util.httpToHttps(res.data.imgurl)
          })
        }
      }
    })
  },
  modifyUserAvatar() {
    this.setData({
      actionSheetVisible: true
    })
  },
  goNextPage(e) {
    var index = e.target.dataset.index;
    wx.navigateTo({
      url: '/pages/other/other?keyIndex=' + index
    })
  },
  handleOpen() {
    this.setData({
      visible: true
    });
  },
  handleSheetClickItem({
    detail
  }) {
    this.setData({
      actionSheetActive: detail.index,
      actionSheetVisible: false
    })
    app.$Toast({
      content: `头像修改为【${this.data.actionSheetList[detail.index].name}】`,
      type: 'success'
    });
    this.getData()
  },
  handleSheetCancel() {
    this.setData({
      actionSheetVisible: false
    })
  },
  handleOk() {
    this.setData({
      visible: false
    });
    wx.clearStorageSync();
    app.$Toast({
      content: '清除缓存成功!'
    });
    this.setData({
      currentSize: wx.getStorageInfoSync().currentSize
    })
  },
  handleCancel() {
    this.setData({
      visible: false
    });
  },
})